import com.ast.AST;
import com.ast.ConstDefNode;
import com.compiler.IRGenerator;
import com.ir.IR;
import com.parser.*;
import com.sysdep.CodeAssembly;
import com.sysdep.CodeGenerator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Compiler {
    public static void main(String[] args) {
        String currentDir = System.getProperty("user.dir");
        String fileName = currentDir + "/" + args[3];
        String outName = currentDir + "/" + args[2];
        // String outName = "D:\\C\\testcase2.s";
        try {
            File file = new File(fileName);
            File out = new File(outName);

            if (!out.exists()) {
                out.createNewFile();
            }
            InputStream inputStream = new FileInputStream(file);
            FileOutputStream outputStream = new FileOutputStream(out);
            // 编译
            Parser parser = new Parser(inputStream);
            IRGenerator irGenerator = new IRGenerator();
            AST ast = parser.parse();
            IR ir = irGenerator.generate(ast);
            CodeGenerator cg = new CodeGenerator();
            CodeAssembly ca = cg.generate(ir);
            // 输出
            outputStream.write(ca.toSource().getBytes());
            outputStream.close();
            inputStream.close();
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }
}